<!-- 后结算模块-财务管理-采购单列表 -->
<template>
  <div class="settlement-list-box">
    <el-container class="template-box">
      <!-- tab搜索 -->
      <tabs
        class="tabs"
        :tabs-arr="indexTabsArr"
        @returnIndex="tabsHandle" />
      <!-- 搜索 -->
      <el-header height="auto">
        <SearchForm
          label-width="110px"
          :data="indexSearchData"
          :compact-data="indexSearchData.slice(0,2)"
          show-collapse
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading='submitLoading'
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="submitLoading"
        :data="dataList"
        height="400"
        :columns="indexTableData"
      >
        <!-- 操作 -->
        <!-- 订单状态（0-全部，21-待提交；22-待供应商响应；23-待供应商发货；24-待收货；25-收货完成；26-已撤消；27-已拒绝） -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            v-if="[24].includes(scope.row.orderStatus)"
            size="small"
            @click.stop="toOrderEdit(scope.row)"
            type="text text-primary">收货</el-button>
          <el-button
            v-if="[21].includes(scope.row.orderStatus)"
            size="small"
            @click.stop="toOrderEdit(scope.row)"
            type="text text-primary">提交</el-button>
          <el-button
            v-if="[22].includes(scope.row.orderStatus)"
            size="small"
            @click.stop="toOrderEdit(scope.row)"
            type="text text-primary">撤销</el-button>
          <el-button
            size="small"
            @click.stop="toDetails(scope.row)"
            type="text text-primary">详情</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
    </el-container>
  </div>
</template>

<script>
import {
  distributorPurchaseList,
  distributorPurchaseOrderOperate,
  distributorPurchaseCount
} from '@const/beforeSettleApi'
import { indexSearchData, indexTableData, indexTabsArr } from './const'
import { hasIn, cloneDeep } from 'lodash'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import tabs from '@components/tabs/index.vue'
export default {
  components: { ElEditable, SearchForm, tabs },
  data () {
    return {
      changePath: `${this.$route.path}/change`,
      readPath: `${this.$route.path}/read`,
      // 搜索标题字段
      indexSearchData,
      // 搜索返回内容
      searchData: {},
      // 表格loading
      tableLoading: false,
      // 列表数据
      dataList: [],
      // 列表表头
      indexTableData,
      // tab字段
      indexTabsArr: cloneDeep(indexTabsArr),
      submitLoading: false,
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      tabIndex: 21
    }
  },
  mounted () {
    this.getList()
    this.getNumber()
  },
  created () {},
  methods: {
    // 获取列表
    async getList () {
      this.submitLoading = true
      const res = await this.$axios({
        method: 'post',
        url: distributorPurchaseList,
        data: {
          ...this.searchData,
          orderStatus: this.tabIndex,
          page: this.curPage,
          pageSize: this.pageSize
        }
      })
      this.dataList = hasIn(res, ['data', 'payload'])
        ? res.data.payload.content || []
        : []
      this.totalElements = res.data.payload.totalElements
      this.submitLoading = false
    },

    // tabs切换处理事件
    tabsHandle (index) {
      this.tabIndex =
        this.indexTabsArr[index].val === 'null'
          ? null
          : this.indexTabsArr[index].val
      this.curPage = 1
      this.getList()
    },

    // 跳转详情
    toDetails (data) {
      const query = {
        id: data.purchaseOrderId
      }
      // 已完成
      this.$router.push({
        path: this.readPath,
        query
      })
    },
    // 去收货 状态一览...
    toOrderEdit (data) {
      const query = {
        id: data.purchaseOrderId
      }
      this.$router.push({
        path: this.changePath,
        query
      })
    },
    // 获取订单数量
    async getNumber () {
      const res = await this.$axios({
        method: 'post',
        url: distributorPurchaseCount
      })
      let arr = hasIn(res, 'data.payload') ? res.data.payload : []
      const cloneIndexTabs = cloneDeep(indexTabsArr)
      cloneIndexTabs.forEach(item => {
        arr.forEach(ite => {
          if (ite.statusCode === item.val) {
            item.badge = { value: ite.count }
          }
        })
        if (item.val === 0) {
          item.badge.value = 0
          arr.forEach(ite => {
            item.badge.value += ite.count
          })
        }
      })
      this.indexTabsArr = cloneIndexTabs
    },

    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.getList()
    },

    // 点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.getList()
    },

    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.settlement-list-box {
  .template-box {
    height: 100%;
    overflow: auto;
    /deep/.tabs .tabs-item {
      &.active {
        .item-font {
          font-size: 14px;
          transform: scale(1.3);
          transition: all 0.3s ease;
        }
      }
    }
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
